package com.company.oa.announcement.controller;

import com.company.oa.announcement.entity.AnnouncementAttachment;
import com.company.oa.announcement.service.AnnouncementAttachmentService;
import com.company.oa.common.api.Result;
import com.company.oa.common.utils.SecurityUtils;
import com.company.oa.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/announcement-attachments")
public class AnnouncementAttachmentController {

    @Autowired
    private AnnouncementAttachmentService attachmentService;

    /**
     * 上传附件
     */
    @PostMapping("/upload")
    @RequiresPermissions("announcement:attachment:upload")
    public Result<Long> uploadAttachment(
            @RequestParam("file") MultipartFile file,
            @RequestParam("announcementId") Long announcementId) {
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();

        Long attachmentId = attachmentService.uploadAttachment(file, announcementId, userId, userName);
        return Result.success(attachmentId);
    }

    /**
     * 获取公告附件列表
     */
    @GetMapping("/list/{announcementId}")
    public Result<List<AnnouncementAttachment>> getAttachmentList(@PathVariable Long announcementId) {
        List<AnnouncementAttachment> attachments = attachmentService.getByAnnouncementId(announcementId);
        return Result.success(attachments);
    }

    /**
     * 删除附件
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("announcement:attachment:delete")
    public Result<Boolean> deleteAttachment(@PathVariable Long id) {
        boolean result = attachmentService.deleteAttachment(id);
        return Result.success(result);
    }

    /**
     * 下载附件
     */
    @GetMapping("/download/{id}")
    public ResponseEntity<Resource> downloadAttachment(@PathVariable Long id, HttpServletRequest request)
            throws IOException {
        AnnouncementAttachment attachment = attachmentService.getById(id);
        if (attachment == null) {
            return ResponseEntity.notFound().build();
        }

        Resource resource = new FileSystemResource(attachment.getPath());

        // 获取文件的内容类型
        String contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        if (contentType == null) {
            contentType = "application/octet-stream";
        }

        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(contentType))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + attachment.getName() + "\"")
                .body(resource);
    }
}